<template>
  <uni-nav-bar title="加注机" color="#333" statusBar></uni-nav-bar>
  <scroll-view
    scroll-y="true"
    enable-back-to-top
    @scrolltolower="loadMore"
    class="scroll-box"
    :style="{ height: pageHeight + 'px' }"
  >
    <view class="filling" @click="tabChange(item.id)" v-for="item in fillList" :key="item.id">
      <view>
        <text>{{ item.code }}</text>
        <text>{{ item.imsi }}</text>
        <text>最新更新时间：{{ item.lasttime }}</text>
      </view>
      <view
        :class="
          item.run_status == '0' ? 'red' : item.run_status == '1' ? 'green' : item.run_status == '-1' ? 'grey' : ''
        "
        @longpress="longpress(item.id, item.is_monitor)"
      >
        {{ item.run_status == '1' ? '正常' : item.run_status == '0' ? '异常' : '停止' }}
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
import { fillingList } from '@/gql/fillStatus';
import { navTo } from '@/utils/navigator';
import { onShow } from '@dcloudio/uni-app';
import { toPublish } from '@mqtt';
import { onMounted, ref } from 'vue';

// static
const pageHeight = ref('');
const fillList = ref([]);
// data
let data = 0;
let lastPage = 1; //总页数
let curPage = 1; //页数
let timer = null as number; //定时器

onMounted(() => {
  //获取可视区域的高度
  uni.getSystemInfo({
    success(res) {
      pageHeight.value = (res.windowHeight - 30).toString();
    },
  });
});

onShow(() => {
  fillList.value = [];
  curPage = 1;
  filling();
});

/**
 * 接收加注机状态信息
 */
function viewDdata() {
  fillList.value = [];
  curPage = 1;
  // 获取页面信息
  filling();
}
/**
 * 长按调整对加注机状态检测
 */
function longpress(id, type) {
  const title = type === 1 ? '是否停止监控' : '是否开启监控';
  uni.showModal({
    title: '温馨提示',
    content: title,
    success(res) {
      if (res.confirm) {
        fillStates(id, type);
        setTimeout(() => {
          viewDdata();
        }, 1000);
      } else if (res.cancel) {
        console.log('用户点击取消');
      }
    },
  });
}
/**
 * 启用停止加注机
 */
function fillStates(id, type) {
  const status = type === 1 ? 0 : 1;
  const payload = {
    fill_id: id,
    status,
  };
  toPublish('jzj/editMonitor', payload, (obj: any) => {
    viewDdata();
  });
}
/**
 * 获取加注机列表
 */
function filling() {
  deltimer();
  manyTimes();
  const payload = {
    query: fillingList,
    variables: {
      page: curPage,
      status: 1,
    },
  };
  toPublish('ql/fill/fillList', payload, (obj: any) => {
    const { fill_list } = obj.data;
    fillList.value = [...fillList.value, ...fill_list.list];
    lastPage = fill_list.lastpage;
  });
}
/**
 * 加载更多
 */
function loadMore() {
  if (curPage < lastPage) {
    curPage++;
    filling();
  }
}
/**
 * 订阅加注机返回信息
 */
function manyTimes() {
  timer = setInterval(() => {
    if (data === 1) {
      deltimer();
    } else {
      fillList.value = [];
      curPage = 1;
      filling();
    }
  }, 60000);
}
/**
 * 清除定时器
 */
function deltimer() {
  if (timer) {
    clearInterval(timer);
    timer = null;
  }
}
/**
 * @param {Object} num 加注机信息
 */
function tabChange(num) {
  data = 1;
  deltimer();
  navTo(`machine/fillingDetail?id=${num}`);
}
</script>

<style lang="less">
.filling {
  width: 95%;
  margin: 2.5%;
  background-color: white;
  border-radius: 5px;
  padding: 20rpx;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  view:nth-of-type(1) {
    width: 80%;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text:nth-of-type(1) {
      font-size: 30rpx;
      font-family: PingFang SC;
      font-weight: bold;
    }
    text:nth-of-type(3) {
      font-size: 20rpx;
      font-family: PingFang SC;
      font-weight: 400;
    }
    text {
      font-size: 24rpx;
      font-family: PingFang SC;
      font-weight: 500;
      color: #333333;
      line-height: 40rpx;
    }
  }
  view:nth-of-type(2) {
    width: 100rpx;
    height: 100rpx;
    border-radius: 50%;
    text-align: center;
    line-height: 100rpx;
    font-size: 28rpx;
    font-family: PingFang SC;
    font-weight: bold;
    color: #ffffff;
  }
  .green {
    background: linear-gradient(-24deg, #2e8df4, #0db5f0);
    box-shadow: 0rpx 2rpx 23rpx 1rpx rgba(204, 204, 204, 0.3);
  }
  .red {
    background: linear-gradient(-24deg, #ff2b2b, #ff2924);
    box-shadow: 0rpx 2rpx 23rpx 1rpx rgba(204, 204, 204, 0.3);
  }
  .grey {
    background: #999;
    box-shadow: 0rpx 2rpx 23rpx 1rpx rgba(204, 204, 204, 0.3);
  }
}
</style>
